package com.cupcorn.balapkarung.state;

import org.andengine.engine.Engine.EngineLock;
import org.andengine.entity.Entity;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.scene.Scene;
import org.andengine.input.touch.TouchEvent;
import org.andengine.input.touch.detector.ClickDetector;
import org.andengine.input.touch.detector.ScrollDetector;

import android.view.KeyEvent;

import com.cupcorn.balapkarung.GameDefine;
import com.cupcorn.lib.Anchor;
import com.cupcorn.lib.define.GameEngineConfiguration;
import com.cupcorn.lib.define.iDefines;
import com.cupcorn.lib.display.DisplaySprite;
import com.cupcorn.lib.game.GameEngine;
import com.cupcorn.lib.game.GameState;

public class StateOption extends GameState implements GameDefine
{
	
	DisplaySprite displaySprite;
	boolean upinside;
	final int animateStep = MAIN_MENU_ANIMATION_STEP;
	int animateCount;
	boolean ready, quitingSubmenu;
	Entity layerMenu;
	
	public StateOption(GameEngine engine)
	{
		super(engine);
	}
	
	public void load()
	{
		displaySprite = new DisplaySprite(engine, GAME_MENU_OPTION);
		for(int i = 0; i < GAME_MENU_OPTION.length; i++)
		{
			displaySprite.newSprite(i, engine.hud, false);
		}
		layerMenu = new Entity();
		layerMenu.attachChild(displaySprite.getSprite(OPTION_BINGKAI_BIRU));
		displaySprite.getSprite(OPTION_BINGKAI_BIRU).addRectangularShape(displaySprite.getSprite(OPTION_SOUND), 0, 20, Anchor.TOP_CENTER);
		displaySprite.getSprite(OPTION_BINGKAI_BIRU).addRectangularShape(displaySprite.getSprite(OPTION_MUSIC), 0, 120, Anchor.TOP_CENTER);
		displaySprite.getSprite(OPTION_SOUND).addRectangularShape(displaySprite.getSprite(OPTION_CEK_SOUND),  Anchor.CENTER_LEFT);
		displaySprite.getSprite(OPTION_MUSIC).addRectangularShape(displaySprite.getSprite(OPTION_CEK_MUSIC), Anchor.CENTER_LEFT);
		displaySprite.getSprite(OPTION_BINGKAI_BIRU).addRectangularShape(displaySprite.getSprite(OPTION_FB), -80, -30, Anchor.BOTTOM_CENTER);
		displaySprite.getSprite(OPTION_BINGKAI_BIRU).addRectangularShape(displaySprite.getSprite(OPTION_TWITTER), 80, -30, Anchor.BOTTOM_CENTER);
		displaySprite.getSprite(OPTION_CEK_SOUND).setX(displaySprite.getSprite(OPTION_CEK_SOUND).getX() + (33 * (GameEngine.CAMERA_WIDTH/800)));
		displaySprite.getSprite(OPTION_CEK_MUSIC).setX(displaySprite.getSprite(OPTION_CEK_MUSIC).getX() + (33 * (GameEngine.CAMERA_WIDTH/800)));
	}
	
	public void init()
	{
		upinside = false;
		animateCount = 0;
		ready = false;
		quitingSubmenu = false;
		engine.hud.attachChild(layerMenu);
		layerMenu.setY(- GameEngine.CAMERA_HEIGHT);
	}
	
	public void update(float deltaTime)
	{
		if(quitingSubmenu)
		{
			if(animateBackTransition())
			{
				engine.nextState(STATE_MAIN_MENU, iDefines.MESSAGE_STATE_UPDATE);
				removeAllObject();
			}
			return;
		}
		if(ready)
		{
			upinside = false;
			return;
		}
		if(animateTransition())
		{
			ready = true;
		}
	}
	
	public void exit()
	{
	}
	
	public boolean keyUp(int keyCode, KeyEvent event)
	{
		if(quitingSubmenu || !ready)
		{
			return false;
		}
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			quitingSubmenu = true;
			animateCount = 0;
		}
		return false;
	}
	
	public void sceneTouch(Scene scene, TouchEvent touchEvent)
	{
		if(!ready || quitingSubmenu)
		{
			return;
		}
		if(touchEvent.isActionUp() && !upinside)
		{
			quitingSubmenu = true;
			animateCount = 0;
		}
	}
	
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent, ITouchArea pTouchArea, float pTouchAreaLocalX, float pTouchAreaLocalY)
	{
		if(pSceneTouchEvent.isActionUp())
		{
			if(pTouchArea.equals(displaySprite.getSprite(0)))
			{
				upinside = true;
			}
			if(pTouchArea.equals(displaySprite.getSprite(OPTION_SOUND)))
			{
				displaySprite.getSprite(OPTION_CEK_SOUND).setVisible(!displaySprite.getSprite(OPTION_CEK_SOUND).isVisible());
				if(displaySprite.getSprite(OPTION_CEK_SOUND).isVisible())
				{
					GameEngineConfiguration.use_sound = true;
					engine.playerSound.play(SOUND_OK);
				}
				else
				{
					GameEngineConfiguration.use_sound = false;
					engine.playerSound.pauseAll();
				}
			}
			if(pTouchArea.equals(displaySprite.getSprite(OPTION_MUSIC)))
			{
				displaySprite.getSprite(OPTION_CEK_MUSIC).setVisible(!displaySprite.getSprite(OPTION_CEK_MUSIC).isVisible());
				if(displaySprite.getSprite(OPTION_CEK_MUSIC).isVisible())
				{
					GameEngineConfiguration.use_music = true;
					engine.playerMusic.resumeAll();
					engine.playerSound.play(SOUND_OK);
				}
				else
				{
					engine.playerMusic.pauseAll();
					GameEngineConfiguration.use_music = false;
				}
			}	
		}
		return false;
	}
	
	public void click(ClickDetector clickDetector, int pointerID, float sceneX, float sceneY)
	{
	}
	
	public void scrollStarted(ScrollDetector scrollDetector, int pointerID, float distanceX, float distanceY)
	{
	}
	
	public void Scroll(ScrollDetector scrollDetector, int pointerID, float distanceX, float distanceY)
	{
	}
	
	public void scrollFinished(ScrollDetector scrollDetector, int pointerID, float distanceX, float distanceY)
	{
	}
	
	private void removeAllObject()
	{
		EngineLock lock = engine.getEngine().getEngineLock();
		lock.lock();
		layerMenu.detachSelf();
		lock.unlock();
	}
	
	boolean animateTransition()
	{
		StateMainMenu.layerSubMenu.setY(StateMainMenu.layerSubMenu.getY() + StateMainMenu.farMove);
		StateMainMenu.displaySprite.getSprite(MAIN_MENU_PLAY).setY(StateMainMenu.displaySprite.getSprite(MAIN_MENU_PLAY).getY() + StateMainMenu.farMove);
		layerMenu.setY(layerMenu.getY() + StateMainMenu.farMove);
		if(animateCount == animateStep)
		{
			return true;
		}
		animateCount++;
		return false;
	}
	
	boolean animateBackTransition()
	{
		StateMainMenu.layerSubMenu.setY(StateMainMenu.layerSubMenu.getY() - StateMainMenu.farMove);
		StateMainMenu.displaySprite.getSprite(MAIN_MENU_PLAY).setY(StateMainMenu.displaySprite.getSprite(MAIN_MENU_PLAY).getY() - StateMainMenu.farMove);
		layerMenu.setY(layerMenu.getY() - StateMainMenu.farMove);
		if(animateCount == animateStep)
		{
			return true;
		}
		animateCount++;
		return false;
	}
}